# 对数据进行基础性的描述性统计分析
import pandas as pd
import numpy as np
from scipy import stats
from tqdm import tqdm


def Base_Describe(data):
    if type(data) == np.ndarray or type(data) == pd.Series:
        describe_serise = pd.Series(data).describe()
        # print(describe_serise)
        # describe_serise.drop('25%')
        # describe_serise.drop('75%')
        # describe_df.index=['有效个案数','平均值','标准差','中位数',]
        data_len = describe_serise['count']
        data_mean = describe_serise['mean']
        # 中位数
        data_center = describe_serise['50%']
        data_std = describe_serise['std']
        data_max = describe_serise['max']
        data_min = describe_serise['min']
        data_skew = describe_serise.skew()
        data_kurt = describe_serise.kurt()
        # 重组
        ReturnData = pd.Series(
            [data_len, data_mean, data_center, data_std, data_max, data_min, data_skew,data_kurt],
            index=['有效个案数', '平均值', '中位数', '标准差', '最大值', '最小值', '偏度', '峰度'])
        return ReturnData
    elif type(data) == pd.DataFrame:
        describe_DataFrame = data.describe().T
        result = []
        used_columns=[]
        for i in tqdm(data.columns.values):
            types = [np.int32, np.int64, np.float64, np.float32]
            # 为数值型才计算
            if types.__contains__(data[i].dtype):
                r = Base_Describe(data[i])
                result.append(r)
                used_columns.append(i)
        result = pd.DataFrame(result,index=used_columns)
        maxcount=max(result['有效个案数'])
        result['缺失个案数']=result['有效个案数']-maxcount
        return result
